package A1_回溯;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LC_216组合总和III {
    LinkedList<Integer> path=new LinkedList<>();
    List<List<Integer>> result=new ArrayList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        backtracking(n,0,k,1);
        return result;
    }
    public void backtracking(int targetsum,int sum,int k,int startIndex){
        if(sum>targetsum)//剪枝
            return;
        if(path.size()==k){
            if(targetsum==sum)
                result.add(new ArrayList<>(path));
            return;
        }
        for(int i=startIndex;i<=(9-(k-path.size())+1);i++){
            sum+=i;
            path.add(i);
            backtracking(targetsum,sum,k,i+1);
            sum-=i;
            path.removeLast();
        }
    }
}
